#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include "TcpSocket.h"

#define MAX_LISENT_NUM 128      // 最大监听数
#define MAX_BUFFER_SIZE 1024    // 最大缓冲区大小


//socket初始化
void TcpServer::createSocket()
{
    /* 初始化服务 */ 
    m_serverfd = socket(AF_INET, SOCK_STREAM, 0);
    if (m_serverfd = -1)
    {
        perror("socket error");
        exit(EXIT_FAILURE);
    }
    /* 启用端口复用 */ 
    int reuseAddr = 1;
    int ret = setsockopt(m_serverfd, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, sizeof(reuseAddr));
    if ( ret == -1)
    {
        perror("setsockopt error");
        close(m_serverfd);
    }
}

/* 绑定 */
void TcpServer::bindSocket()
{
    int ret = bind(m_serverfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (ret == -1)
    {
        perror("bind error");
        exit(EXIT_FAILURE);
    }
}

/* 监听 */
void TcpServer::listenSocket()
{
    int ret = listen(m_serverfd, MAX_LISENT_NUM);
    if (ret == -1)
    {
        perror("listen error");
        exit(EXIT_FAILURE);
    }
}

void TcpServer::acceptConnection()
{
    struct sockaddr_in client_addr;
    socklen_t client_len = sizeof(client_addr);
    int client_fd = accept(m_serverfd, (struct sockaddr *)&client_addr, &client_len);
    if (client_fd == -1) 
    {
        perror("Failed to accept client connection");
        exit(EXIT_FAILURE);
    }

}

void TcpServer::start()
{
    
    int addrlen = sizeof(server_addr);
    int new_socket;
    if ((new_socket = accept(m_serverfd, (struct sockaddr *)&server_addr, (socklen_t*)&addrlen))<0) 
    {
        std::cerr << "Accept failed" << std::endl;
    }
    
    // 读取客户端请求
    char buffer[1024] = {0};
    int valread = read(new_socket, buffer, 1024);
    std::cout << "Received: " << buffer << std::endl;

    // 发送响应
    std::string response = "Hello from server";
    send(new_socket, response.c_str(), response.length(), 0);
    std::cout << "Response sent" << std::endl;
}

TcpServer::~TcpServer()
{
    if(m_serverfd != -1)
    {
        close(m_serverfd);
    }
}

TcpClient::TcpClient(const std::string &serverIP, int serverPort)
:m_sockfd(-1),m_serverIP(serverIP), m_serverPort(serverPort)
{
    m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (m_sockfd == -1) 
    {
        perror("Failed to create socket");
        exit(EXIT_FAILURE);
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(m_serverPort);

    int ret = inet_pton(AF_INET, m_serverIP.c_str(), &server_addr.sin_addr);
    if ( ret != 1) 
    {
        perror("Invalid address/ Address not supported");
        exit(EXIT_FAILURE);
    }

    ret = connect(m_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if ( ret == -1) 
    {
        perror("Connection Failed");
        exit(EXIT_FAILURE);
    }

}

TcpClient::~TcpClient()
{
    if(m_sockfd != -1)
    {
        close(m_sockfd);
    }
}


ServerAddress::ServerAddress(const std::string &ip, int port)
{
    server_addr.sin_family = AF_INET;   /* 地址族 */
    server_addr.sin_port = htons(port); /* 端口需要转成大端 */
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);  /* ip地址需要转成大端 */
}

const sockaddr_in &ServerAddress::getServerAddress() const
{
    return server_addr;
}
